package org.ssp.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.ssp.base.ResultSetHanlder;
import org.ssp.dao.BackupDao;
import org.ssp.pojo.Backup;
import org.ssp.util.DBUtil;

public class BackupImpl implements BackupDao{
   //1.给用户添加备注
	@Override
	public void addBackup(Backup back) {
		String sql="insert into backupforuser(content,datetime,uid) values(?,?,?);";
		DBUtil.update(sql, back.getContent(),back.getDatetime(),back.getUid());
	}
	//2.给活动添加备份
	@Override
	public void addBackup2(Backup back){
		String sql="insert into backupfortravel(content,datetime,travelid,assistid) values(?,now(),?,?);";
		DBUtil.update(sql, back.getContent(),back.getTravelid(),back.getAssistid());
	}
   //3，查找某一用户的备份列表
	@Override
	public ArrayList<Backup> findByUid(String uid,int offset,int rows) {
		String sql="select * from backupforuser where uid=? limit ?,?;";
		return (ArrayList<Backup>)DBUtil.query(sql,  new ResultSetHanlder(){

			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<Backup> list=new ArrayList<Backup>();
				try {
					while(rs.next()){
						Backup back=new Backup();
						back.setId(rs.getInt("id"));
						back.setContent(rs.getString("content"));
						back.setDatetime(rs.getString("datetime"));
						back.setUid(rs.getString("uid"));
						list.add(back);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return list;
			}
		}, uid,offset,rows);
	}
	//4.查找某一用户的备份列表的总数
	@Override
	public int findByUid(String uid) {
		String sql="select * from backupforuser where uid=?;";
		return (Integer)DBUtil.query(sql,  new ResultSetHanlder(){

			@Override
			public Object doHandler(ResultSet rs) {
				Integer total=0;
				try {
					while(rs.next()){
						total+=1;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return total;
			}
		},uid);
	}
	 //5，查找某一用户的备份列表
		@Override
		public ArrayList<Backup> findByTravelid(int travelid,int offset,int rows) {
			String sql="select * from backupfortravel where travelid=? limit ?,?;";
			return (ArrayList<Backup>)DBUtil.query(sql,  new ResultSetHanlder(){

				@Override
				public Object doHandler(ResultSet rs) {
					ArrayList<Backup> list=new ArrayList<Backup>();
					try {
						while(rs.next()){
							Backup back=new Backup();
							back.setId(rs.getInt("id"));
							back.setContent(rs.getString("content"));
							back.setDatetime(rs.getString("datetime"));
							back.setTravelid(rs.getInt("travelid"));
							back.setAssistid(rs.getString("assistid"));
							list.add(back);
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return null;
					}
					return list;
				}
			}, travelid,offset,rows);
		}
		//6.查找某一用户的备份列表的总数
		@Override
		public int findByTravelid(int travelid) {
			String sql="select * from backupfortravel where travelid=?;";
			return (Integer)DBUtil.query(sql,  new ResultSetHanlder(){

				@Override
				public Object doHandler(ResultSet rs) {
					Integer total=0;
					try {
						while(rs.next()){
							total+=1;
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return null;
					}
					return total;
				}
			},travelid);
		}
	
	//7，删除备份
	@Override
	public void deleteBackup(int id) {
		String sql="delete from backupforuser where id=?;";
		DBUtil.update(sql, id);
	}
	//8，删除备份
	@Override
	public void deleteBackup2(int id) {
		String sql="delete from backupfortravel where id=?;";
		DBUtil.update(sql, id);
	}
	//9，修改用户的备份信息
	@Override
	public void updateBackup(Backup back){
		String sql="update backupforuser set content=?,datetime=? where id=?;";
		DBUtil.update(sql, back.getContent(),back.getDatetime(),back.getId());
	}
	//10.修改活动的备份信息
	@Override
	public void updateBackup2(Backup back){
		String sql="update backupfortravel set content=?,datetime=? where id=?;";
		DBUtil.update(sql, back.getContent(),back.getDatetime(),back.getId());
	}

}
